// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.reports.editor.design.parts;

import java.beans.PropertyChangeEvent;
import java.util.Iterator;
import java.util.List;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.reports.editor.design.border.SectionBorder;
import org.dengues.reports.editor.design.figures.TableFigure;
import org.dengues.reports.editor.design.handlers.AbstractGuideHandle;
import org.dengues.reports.editor.design.handlers.TableGuideHandle;
import org.dengues.reports.editor.design.layers.TableGridLayer;
import org.dengues.reports.editor.design.layout.TableLayout;
import org.dengues.reports.editor.design.models.RepTableCellDiagram;
import org.dengues.reports.editor.design.models.RepTableDiagram;
import org.dengues.reports.i18n.Messages;
import org.eclipse.draw2d.FreeformLayer;
import org.eclipse.draw2d.FreeformLayeredPane;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LayeredPane;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.editparts.GridLayer;
import org.eclipse.gef.editparts.GuideLayer;

/**
 * <p>
 * Table EditPart,control the UI & model of table
 * </p>
 * 
 * 
 * /** Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-2-22 qiang.zhang $
 * 
 */
public class TableEditPart extends AbstractTableEditPart {

    public static final String GUIDEHANDLE_TEXT = Messages.getString("TableEditPart.guideText"); //$NON-NLS-1$

    /**
     * Constructor
     * 
     * @param obj
     */
    public TableEditPart(Object obj) {
        super(obj);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractEditPart#getModelChildren()
     */
    @Override
    protected List getModelChildren() {
        List<RepTableCellDiagram> repTableCells = ((RepTableDiagram) getModel()).getRepTableCells();
        return repTableCells;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.birt.report.designer.internal.ui.editors.schematic.editparts.ReportElementEditPart#createGuideHandle()
     */
    protected AbstractGuideHandle createGuideHandle() {
        TableGuideHandle handle = new TableGuideHandle(this);
        handle.setIndicatorLabel(getGuideLabel());
        handle.setIndicatorIcon(ImageUtil.getImage(IDenguesSharedImage.VIEW_TABLE));
        return handle;
    }

    public String getGuideLabel() {
        return GUIDEHANDLE_TEXT;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
     */
    protected void createEditPolicies() {
        // installEditPolicy(EditPolicy.COMPONENT_ROLE, new ReportComponentEditPolicy() {
        //
        // installEditPolicy(EditPolicy.CONTAINER_ROLE, new ReportContainerEditPolicy());
        // should add highlight policy
        // installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE, new
        // ContainerHighlightEditPolicy());
        // installEditPolicy(EditPolicy.LAYOUT_ROLE, new TableXYLayoutEditPolicy((XYLayout)
        // getContentPane().getLayoutManager()));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.reports.editor.design.parts.ReportElementEditPart#refreshFigure()
     */
    public void refreshFigure() {
        getFigure().setBorder(getFigure().getBorder());

        ((SectionBorder) (getFigure().getBorder())).setPaddingInsets(getPadding(getFigure().getInsets()));

        // refreshBackground((DesignElementHandle) getModel());
        // refreshMargin();

        for (Iterator itr = getChildren().iterator(); itr.hasNext();) {
            TableCellEditPart fg = (TableCellEditPart) itr.next();
            if (!fg.isDelete()) {
                fg.updateBlankString();
            }
        }
        layoutManagerLayout();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getPadding".
     * 
     * @param insets
     * @return
     */
    private Insets getPadding(Insets retValue) {
        return new Insets(1);
    }

    private void layoutManagerLayout() {
        ((TableLayout) getContentPane().getLayoutManager()).markDirty();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
     */
    protected IFigure createFigure() {
        TableFigure viewport = new TableFigure();
        viewport.setOpaque(false);
        // viewport.setImage(ImageUtil.getImage(IDenguesSharedImage.QUERSTION));
        innerLayers = new FreeformLayeredPane();
        createLayers(innerLayers);
        viewport.setContents(innerLayers);
        return viewport;
    }

    /**
     * Creates the top-most set of layers on the given layered pane.
     * 
     * @param layeredPane the parent for the created layers
     */
    protected void createLayers(LayeredPane layeredPane) {
        layeredPane.add(createGridLayer(), GRID_LAYER);
        layeredPane.add(getPrintableLayers(), PRINTABLE_LAYERS);
        layeredPane.add(new FreeformLayer(), HANDLE_LAYER);
        layeredPane.add(new GuideLayer(), GUIDE_LAYER);
    }

    /**
     * Creates a {@link GridLayer grid}. Sub-classes can override this method to customize the appearance of the grid.
     * The grid layer should be the first layer (i.e., beneath the primary layer) if it is not to cover up parts on the
     * primary layer. In that case, the primary layer should be transparent so that the grid is visible.
     * 
     * @return the newly created GridLayer
     */
    protected GridLayer createGridLayer() {
        GridLayer grid = new TableGridLayer(this);
        grid.setOpaque(false);
        return grid;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractEditPart#eraseTargetFeedback(org.eclipse.gef.Request)
     */
    public void eraseTargetFeedback(Request request) {
        if (isActive()) {
            this.getViewer().setCursor(null);
        }
        super.eraseTargetFeedback(request);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#addChildVisual(org.eclipse.gef.EditPart, int)
     */
    protected void addChildVisual(EditPart part, int index) {
        // make sure we don't keep a select cell cursor after new contents
        // are added
        this.getViewer().setCursor(null);
        super.addChildVisual(part, index);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.reports.editor.design.layout.ITableLayoutOwner#getColumnCount()
     */
    public int getColumnCount() {
        return 3;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.reports.editor.design.layout.ITableLayoutOwner#getColumnWidth(int)
     */
    public DimensionInfomation getColumnWidth(int number) {
        return new DimensionInfomation(0.0, null);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.reports.editor.design.layout.ITableLayoutOwner#getColumnWidthValue(int)
     */
    public int getColumnWidthValue(int number) {
        return 4;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.reports.editor.design.layout.ITableLayoutOwner#getDefinedWidth()
     */
    public String getDefinedWidth() {
        return "100.0%";
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.reports.editor.design.layout.ITableLayoutOwner#getRawWidth(int)
     */
    public String getRawWidth(int columNumber) {
        return "";
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.reports.editor.design.layout.ITableLayoutOwner#getRowCount()
     */
    public int getRowCount() {
        return 3;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.reports.editor.design.layout.ITableLayoutOwner#getRowHeight(int)
     */
    public DimensionInfomation getRowHeight(int number) {
        return new DimensionInfomation(0.0, null);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.reports.editor.design.layout.ITableLayoutOwner#getRowHeightValue(int)
     */
    public int getRowHeightValue(int number) {
        return 23;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
     */
    public void propertyChange(PropertyChangeEvent evt) {

    }

}